package cn.play.cardgame.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.play.cardgame.DidiAudioService;
import cn.play.cardgame.R;
import cn.play.cardgame.SoundService;
import cn.play.cardgame.constant.Constant;
import cn.play.cardgame.constant.Constant.GAMESTATUS;
import cn.play.cardgame.event.CallbackEvent;
import cn.play.cardgame.util.CountDownTimer;
import cn.play.cardgame.util.ImageUtil;
import de.greenrobot.event.EventBus;

public class StatusPanel extends FrameLayout {
    @InjectView(R.id.iv_count_down_timer)
    ImageView mIvCountDown;
    @InjectView(R.id.game_status)
    TextView mCameraStatus;
    private Context mContext;
    private MyCountDownTimer countDownTimer;
    private GAMESTATUS mGameStatus = GAMESTATUS.GAME_IDLE;
    private static final int DEFAULT_SEC = 10000;
    private static final int COUNT_DOWN_INTERVAL = 1000;
    private boolean isStartDiDi = false;

    public StatusPanel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        countDownTimer = new MyCountDownTimer(DEFAULT_SEC, COUNT_DOWN_INTERVAL);
        initViews();
    }

    public StatusPanel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusPanel(Context context) {
        this(context, null);
    }

    private void initViews() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);

        View view = View.inflate(mContext, R.layout.status_panel, null);
        ButterKnife.inject(this, view);
        params.gravity = Gravity.CENTER;
        this.addView(view, params);
    }

    public void setCameraStatus(String cameraStatus) {
        mCameraStatus.setText(cameraStatus);
    }

    public void start() {
        countDownTimer.start();
    }

    public void cancel() {
        countDownTimer.cancel();
    }

    public void setMillisInFuture(long millisInFuture) {
        countDownTimer.setMillisInFuture(millisInFuture);
    }

    public boolean isFiveTimeMillisLeft() {
        return countDownTimer.isFiveTimeMillisLeft();
    }

    public GAMESTATUS getCurrentStatus() {
        return mGameStatus;
    }

    /*
     * 准备中 等候下局开始 请押分 计时 xx 开奖中 计时 xx 准备中 计时 xx
     */
    public void switchStatus(GAMESTATUS status) {
        mGameStatus = status;
        switch (status) {
            case GAME_IDLE:
                break;
            case GAME_END:
                break;
            case GAME_PREPARE:
                // 通知CameraPanel底部信息
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CAMERA_BOTTOM_STATUS, "准备中"));
                // 通知BetPanel是否可以点击
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_BET, "1"));
                break;
            case GAME_PLAY:
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_SHOW_TIPS, ""));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CAMERA_BOTTOM_STATUS, "请押分"));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_BET, "2"));
                break;
            case GAME_LOTTERY:
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_HIDE_TIPS, ""));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CAMERA_BOTTOM_STATUS, "计算盈利中"));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_BET, "3"));
                break;
            case GAME_SPACE:
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CAMERA_BOTTOM_STATUS, "间隔时间"));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_BET, "4"));
                break;
            case GAME_PAUSE:
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_HIDE_TIPS, ""));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CAMERA_BOTTOM_STATUS, "开奖中"));
                EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_BET, "5"));
                break;
        }
    }

    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            cancel();
            setCountDown(0);
            if (mGameStatus == GAMESTATUS.GAME_PREPARE) {
            } else if (mGameStatus == GAMESTATUS.GAME_PLAY) {
            } else if (mGameStatus == GAMESTATUS.GAME_LOTTERY) {
            } else if (mGameStatus == GAMESTATUS.GAME_SPACE) {
            }
        }

        private void setCountDown(long millisUntilFinished) {
            mIvCountDown.setBackgroundDrawable(ImageUtil.getInstance()
                    .bitmapToDrawable(mContext,
                            ImageUtil.getCountDownMapping(),
                            String.format("%02d", millisUntilFinished / 1000)));
        }

        @SuppressWarnings("deprecation")
        @Override
        public void onTick(long millisUntilFinished) {
            //pchen 2017.07.24 1111 该功能暂时取消不播放
//            if (mGameStatus == GAMESTATUS.GAME_PLAY) {
//                long mTime = millisUntilFinished / 1000;
//                if (mTime <= 5) {
//                    if (!isStartDiDi) {
//                        playDidi(true);
//                    }
//                }
//            } else {
//                if (isStartDiDi) {
//                    playDidi(false);
//                }
//            }

            setCountDown(millisUntilFinished);
        }
    }
//
//    private void playDidi(boolean isStart) {
//        isStartDiDi = isStart;
//
//        if (isStart) {
//            //先关闭当前的背景音乐
//            Intent mediaService = new Intent(mContext, SoundService.class);
//            mediaService.putExtra("SONG", 1);
//            mediaService.putExtra("MSG", SoundService.STOP_MSG);
//            mContext.startService(mediaService);
//
//            Intent didiMediaService = new Intent(mContext, DidiAudioService.class);
//            didiMediaService.putExtra("SONG", 1);
//            didiMediaService.putExtra("MSG", 1);
//            mContext.startService(didiMediaService);
//        } else {
//            //先关闭当前的didi音乐
//            Intent didiMediaService = new Intent(mContext, DidiAudioService.class);
//            didiMediaService.putExtra("SONG", 3);
//            didiMediaService.putExtra("MSG", 3);
//            mContext.startService(didiMediaService);
//
//            Intent mediaService = new Intent(mContext, SoundService.class);
//            mediaService.putExtra("SONG", 1);
//            mediaService.putExtra("MSG", SoundService.PLAY_MSG);
//            mContext.startService(mediaService);
//        }
//
//
//    }
}
